/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package businesscard.DAO;

import bc.entity.Bophan;
import bc.entity.Quanhuyen;
import bc.entity.Thanhpho;
import bc.util.HibernateUtil;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author Welcome
 */
public class QuanhuyenDAO {

    private Session session;

    public QuanhuyenDAO() {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        this.session = factory.getCurrentSession();
    }

    public Quanhuyen[] selectQuanhuyen(int id_thanhpho) {
        try {
            session.getTransaction().begin();
            String sql = "from Quanhuyen where id_Thanhpho = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, id_thanhpho);
            List result = query.list();
            Quanhuyen[] quanhuyenList = new Quanhuyen[result.size()];
            result.toArray(quanhuyenList);
            session.flush();
            session.getTransaction().commit();
            return quanhuyenList;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;

    }
//cua nga

    public Quanhuyen getQuanhuyenbyId(int id) {
        session.beginTransaction().begin();
        String sql = "from Quanhuyen where idquanhuyen=?";
        try {
            Query query = session.createQuery(sql);
            query.setInteger(0, id);
            Quanhuyen qh = (Quanhuyen) query.uniqueResult();
            session.flush();
            return qh;

        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }

        return null;

    }

    public List<Quanhuyen> getQuanhuyenByTP(Thanhpho tp) {
        try {

            session.getTransaction().begin();
            String sql = "from Quanhuyen where thanhpho = ?";
            Query query = session.createQuery(sql);
            query.setEntity(0, tp);
            List result = query.list();
            //Quanhuyen[] quanhuyenList = new Quanhuyen[result.size()];
            //   result.toArray(quanhuyenList);
            session.flush();
            return result;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public List<Quanhuyen> getQuanhuyenLikeName(String name) {
        session.beginTransaction().begin();
        String sql = "from Quanhuyen where quanhuyen like ?";
        try {
            Query query = session.createQuery(sql);
            query.setString(0, "%" + name + "%");
            List result = query.list();
            session.flush();
            return result;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        QuanhuyenDAO A = new QuanhuyenDAO();
        Quanhuyen[] B = A.selectQuanhuyen(20);
        for (int i = 0; i < B.length; i++) {
            System.out.println("a ... " + B[i].getQuanhuyen());
        }
    }
}
